<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>部门信息</title>
    <link rel="stylesheet" href="../static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../static/css/public.css" media="all">
    <script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
</head>
<body>
<form id="addType" class="layui-form" action="" method="get">
    <input type="hidden" id="departmentId" name="departmentId"/>
    <div class="layui-form-item" style="margin-top: 50px">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input id="departmentName" name="departmentName"  lay-verify="required" type="text" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text" style="margin-top: 30px">
        <label class="layui-form-label">部门描述</label>
        <div class="layui-input-block">
            <textarea id="description" name="description" class="layui-textarea"  lay-verify="required" cols="62" rows="6"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" style="margin-left: 280px">
            <button id="subbtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn"></button>
        </div>
    </div>
</form>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script th:inline="javascript" type="text/javascript">
    let data={
        departmentId: null,
        departmentName: null,
        description: null,
    }
    $(function (){
        let department=[[${department}]];
        if(department===null){
            $('#subbtn').html("确认添加");
            submitUrl="/department/addDepartment";
        }else{
            $('#departmentId').val(department.departmentId);
            $('#departmentName').val(department.departmentName);
            $('#description').val(department.description);
            $('#subbtn').html("确认修改");
            submitUrl="/department/editDepartment";
        }

        $('#addType').submit(function (e){
            let formData=new FormData(this);
            data.departmentId=formData.get("departmentId");
            data.departmentName=formData.get("departmentName");
            data.description=formData.get("description");
            $.ajax({
                type:"post",
                url:submitUrl,
                data:data,
                success:function (res){
                    if(res.code===200){
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                        let index=parent.layer.getFrameIndex(window.name);
                        setTimeout(function (){
                            window.parent.location.reload();//刷新父页面
                            parent.layer.close(index);
                        },1500);
                    }else{
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                    }
                },
                error:function (error){
                    layer.msg(error,{time:5000,icon:2,offset:[15]})
                }
            })
            e.preventDefault();
        })



    })
</script>
</body>
</html>